﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PMS
{
    public partial class frmEmployeeInfo : Form
    {
        public string YGName;
        public string YGID;
        DBOperate operate = new DBOperate();
        public frmEmployeeInfo()
        {
            InitializeComponent();
        }

        private void frmEmployeeInfo_Load(object sender, EventArgs e)
        {
            operate.BindDropdownlist("tb_department", cbbYGBumen, 1);       //绑定下拉列表
            operate.BindDropdownlist("tb_userJob", txtYGZhiwu, 1);          //绑定所有职务列表
            this.Text = "[" + YGName + "]的个人信息";                       //设置窗体标题
            string sql =
                //创建SQL字符串
                "select * from tb_employee where employeeID='" + YGID + "'";
            DataSet ds = operate.GetTable(sql);                             //得到数据集
            ds.Dispose();                                                   //释放资源
            txtYGNum.Text = ds.Tables[0].Rows[0][1].ToString();             //获取员工编号信息
            txtYGName.Text = ds.Tables[0].Rows[0][2].ToString();            //获取员工姓名信息
            cbbYGSex.SelectedItem = ds.Tables[0].Rows[0][3].ToString();     //获取性别信息
            cbbYGBumen.SelectedItem = ds.Tables[0].Rows[0][4].ToString();   //获取部门信息
            txtYGBirthday.Text = ds.Tables[0].Rows[0][5].ToString();        //获取生日信息
            txtYGminzu.SelectedItem = ds.Tables[0].Rows[0][6].ToString();   //获取民族信息
            cbbYGHunyin.SelectedItem = ds.Tables[0].Rows[0][7].ToString();  //获取婚姻信息
            txtYGZhiwu.SelectedItem = ds.Tables[0].Rows[0][8].ToString();   //获取职务信息
            txtYGPhone.Text = ds.Tables[0].Rows[0][9].ToString();           //获取电话信息
            txtYGJiuzhi.Text = ds.Tables[0].Rows[0][10].ToString();         //获取就职日期
            txtYGPay.Text = ds.Tables[0].Rows[0][11].ToString();            //获取工资信息
            operate.Get_Image(YGID, pictureBox1);                           //获取图像信息
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            try
            {
                if(txtYGBirthday.Text.Trim() == ""||
                    txtYGJiuzhi.Text.Trim() == ""||
                    txtYGminzu.Text.Trim() == ""||
                    txtYGName.Text.Trim() == ""||
                    txtYGNum.Text.Trim() == ""||
                    txtYGPhone.Text.Trim() == ""||
                    txtYGZhiwu.Text.Trim() == ""||
                    txtYGPay.Text.Trim() == "")
                {
                    MessageBox.Show("请将信息填写完整", "警告",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                else
                {
                    if(txtYGPhone.Text.Length!=11)
                    {
                        MessageBox.Show("手机号码为11位");
                        return;
                    }
                    else
                    {
                        string strUpdateSql =
                            "update tb_employee set employeeName='" + txtYGName.Text.Trim() + "',employeeSex='" + cbbYGSex.Text + "',employeeDept='" + cbbYGBumen.Text + "',employeeBirthday='" + txtYGBirthday.Text + "',employeeNation='" +
                            txtYGminzu.Text.Trim() + "',employeeMarriage'" + cbbYGHunyin.Text + "',employeeDuty='" + txtYGZhiwu.Text.Trim() +
                            "',employeePhone='" + txtYGPhone.Text.Trim() + "',employeeAccession='" + txtYGJiuzhi.Text + "',employeePay='" + txtYGPay.Text.Trim() + "'where employeeID='" + YGID + "'";
                        int num = operate.OperateData(strUpdateSql);                        //更新数据库信息
                        if(openFileDialog1.FileName == "openFileDialog1")
                        {

                        }
                        else
                        {
                            operate.SaveImage(this.txtYGNum.Text.Trim(), openFileDialog1);  //保存图像信息
                        }
                        if(num > 0)
                        {
                            string update1 =
                                "update tb_redeploy set UName='" + txtYGName.Text + "',UOldDep='" + cbbYGBumen.Text + "',UOldJob='" + txtYGZhiwu.Text + "',UOldPay='" + txtYGPay.Text + "'where UID='" + txtYGNum.Text + "'";
                            string update2 =
                                "update tb_prize set UserName='" + txtYGName.Text + "',UserDep='" + cbbYGBumen.Text + "'where UserID='" + txtYGNum.Text + "'";
                            string update3 =
                                "update tb_pay set YName='"+ txtYGName.Text+"',YSex='"+cbbYGSex.Text+"',Ydep='"+cbbYGBumen.Text+"',YZhiwu='"+txtYGZhiwu.Text+"',YBasePay='"+txtYGPay.Text+"'where YID='"+txtYGNum.Text+"'";
                            string update4 =
                                "update tb_check set Pname='" + txtYGName.Text + "',Pdep='" + cbbYGBumen.Text + "'where PID='" + txtYGNum.Text + "'";
                            operate.OperateData(update1);           //更新数据库
                            operate.OperateData(update2);
                            operate.OperateData(update3);
                            operate.OperateData(update4);
                            MessageBox.Show("员工信息修改成功", "提示",
                                MessageBoxButtons.OK, MessageBoxIcon.Information);
                            this.Close();
                        }
                    }
                }
            }
            catch(Exception EX)
            {
                MessageBox.Show(EX.Message, "提示",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string strg = Application.StartupPath.ToString();           //得到应用程序路径信息
            strg = strg.Substring(0, strg.LastIndexOf("\\"));           //截取路径信息
            strg = strg.Substring(0, strg.LastIndexOf("\\"));
            strg += @"\PMSImage";                                       //添加路径信息
            strg += @"\default.jpg";                                    //添加文件名称
            openFileDialog1.FileName = strg;                            //设置打开文件路径信息
            pictureBox1.Image = System.Drawing.Image.                   //显示图像信息
                FromFile(openFileDialog1.FileName);
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            try
            {
                string DelSql =
                    "delete from tb_employee where employeeID='" + YGID + "'";
                operate.OperateData(DelSql);                            //删除数据
                MessageBox.Show(
                    "删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            catch
            {
                MessageBox.Show("删除操作失败", "提示",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}
